package org.springblade.modules.repair.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.repair.dto.MaintainTimeDTO;
import org.springblade.modules.repair.dto.MaintainTimeSubmitDTO;
import org.springblade.modules.repair.entity.BuildingMaintainTime;
import org.springblade.modules.repair.mapper.BuildingMaintainTimeMapper;
import org.springblade.modules.repair.service.IBuildingMaintainTimeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 项目保养作业时间段表 服务实现类
 *
 * @author ysq
 * @since 2023-08-29
 */
@Service
public class BuildingMaintainTimeServiceImpl extends BaseServiceImpl<BuildingMaintainTimeMapper, BuildingMaintainTime> implements IBuildingMaintainTimeService {

	@Override
	public List<MaintainTimeSubmitDTO> selectMaintainTimes(Long buildingId) {
		List<BuildingMaintainTime> list = list(Wrappers.<BuildingMaintainTime>lambdaQuery().eq(BuildingMaintainTime :: getBuildingId, buildingId));
		Map<Integer, List<BuildingMaintainTime>> map = list.stream().collect(Collectors.groupingBy(t -> t.getWeek()));

		// 数据封装
		List<MaintainTimeSubmitDTO> data = new ArrayList<>(7);
		map.forEach((k,v) -> {
			MaintainTimeSubmitDTO item = new MaintainTimeSubmitDTO();
			item.setWeek(k);
			int type = v.get(0).getType();
			item.setType(type);
			if (type != 0) {
				List<MaintainTimeDTO> maintainTimeList = new ArrayList<>(5);
				for (BuildingMaintainTime buildingMaintainTime : v) {
					MaintainTimeDTO maintainTime = new MaintainTimeDTO();
					maintainTime.setStartTime(buildingMaintainTime.getStartTime());
					maintainTime.setEndTime(buildingMaintainTime.getEndTime());
					maintainTimeList.add(maintainTime);
				}
				item.setMaintainTimeList(maintainTimeList);
			}
			data.add(item);
		});

		return data;
	}
}
